package com.myapp.db.config.define;

import java.io.File;
import java.util.Collection;

import org.apache.log4j.Logger;

import com.myapp.data.global.Instances;
import com.myapp.db.config.ann.TableDefine;

public class DBConfigManager {
	// 日志
	private Logger log = Logger.getLogger(DBConfigManager.class);

	private static DBConfigManager dbConfigManager = null;

	public static DBConfigManager getInstance() {
		if (dbConfigManager == null) {
			dbConfigManager = new DBConfigManager();
		}
		return dbConfigManager;
	}

	/**
	 * 初始化db配置
	 */
	public void initDBConfig() {
		// 清空容器
		Instances.getDBConfigContainer().clear();
		// 重新加载
		File dbConfigBeanPath = new File(Instances.getAppConfigBean()
				.getCurrentPath()
				+ Instances.getAppConfigBean().getDbConfigBeanPath());
		this.initPath(dbConfigBeanPath);
		// 创建表信息
		this.initTable();
	}

	private void initTable() {
		// 创建表
		Collection<DBConfigBean> dbConfigBeans = Instances
				.getDBConfigContainer().getAllDBConfigBean();
		for (DBConfigBean dbConfigBean : dbConfigBeans) {
			dbConfigBean.getDao().createTable();
		}
		// 获取表中最大数据
		long maxId = 0;
		for (DBConfigBean dbConfigBean : dbConfigBeans) {
			maxId = 0;
			maxId = dbConfigBean.getDao().getMaxId();
			Instances.getIDRecord().addTableMaxId(
					dbConfigBean.getEntityClass(), maxId);
		}
	}

	private void initPath(File path) {
		if (path.isDirectory()) {
			File[] files = path.listFiles();
			for (File file : files) {
				this.initPath(file);
			}
		} else {
			this.initFile(path);
		}
	}

	@SuppressWarnings("unchecked")
	private void initFile(File file) {
		String path = file.getAbsolutePath();
		int start = path.indexOf("com");
		if (start != -1) {
			String className = path.substring(start);
			if (className.endsWith(".class")) {
				className = className.substring(0, className.length() - 6);
			}
			className = className.replaceAll("/", ".");
			className = className.replaceAll("\\\\", ".");
			TableDefine tableDefine = null;
			try {
				@SuppressWarnings("rawtypes")
				Class clazz = Class.forName(className);
				if (clazz.isAnnotationPresent(TableDefine.class)) {
					tableDefine = (TableDefine) clazz
							.getAnnotation(TableDefine.class);
					// 初始化配置bean
					DBConfigBean dbConfigBean = new DBConfigBean();
					dbConfigBean.setDao(tableDefine.dao().newInstance());
					dbConfigBean.setName(tableDefine.name());
					dbConfigBean.setEntityClass(clazz);
					// 保存配置
					Instances.getDBConfigContainer().addDBConfigBean(
							dbConfigBean);
				}
			} catch (ClassNotFoundException e) {
				log.error(String.format("Class[%s] not found", path), e);
				System.exit(1);
			} catch (InstantiationException e) {
				log.error(String.format("Can not init Dao[%s]",
						tableDefine.dao()), e);
				System.exit(1);
			} catch (IllegalAccessException e) {
				log.error(String.format("Can not init Dao[%s]",
						tableDefine.dao()), e);
				System.exit(1);
			}
		}
	}
}
